//格式化字符串，使用方式: 'Hello. My name is {0} {1}'.format(firstName, lastName);
String.prototype.format = function () {
    var args = arguments;
    return this.replace(/\{(\d+)\}/g,
        function (m, i) {
            return args[i];
        });
};

//去掉前后的空格，使用方式: ' Hello '.trim();
String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
};

//在JS的StringBuilder类型，为处理大字符串使用，使用方式如下：
//var str = new StringBuilder();
//str.append('xxxxxxxxxxxxxxx');
//str.append('xxxxxxxxxxxxxxx');
//alert(str.toString());
function StringBuilder() {
    var strArray = new Array();
    this.append = function (str) { strArray.push(str); return this; };
    this.toString = function () { return strArray.join(""); };
}

String.prototype.isMoney = function () {
    /// <summary>
    ///     判断是否为金额，支持正负(+-)
    /// </summary>
    return this.match(/^(-)?(([1-9]{1}\d*)|([0]{1}))((\.[0-9][1-9])|(\.[1-9]{1}))?$/g);
}

String.prototype.isMoney2 = function () {
    /// <summary>
    ///     判断是否为金额，只支持正数
    /// </summary>
    return this.match(/^(-)?(([1-9]{1}\d*)|([0]{1}))((\.[0-9][1-9])|(\.[1-9]{1}))?$/g);
}

String.prototype.isInt = function () {
    /// <summary>
    ///     判断是否为整数，支持正负(+-)，第一位不能为0
    /// </summary>
    return this.match(/^(-)?[1-9]{1}\d*$/g);
}

String.prototype.isPint = function () {
    /// <summary>
    ///     判断是否为正整数
    /// </summary>
    return this.match(/^[1-9]\d*$/g);
}

String.prototype.isUint = function () {
    /// <summary>
    ///     判断是否为无符号整数
    /// </summary>
    return this.match(/^(0?)$|^([1-9]{1}\d*)$/g);
}

String.prototype.isEmpty = function () {
    /// <summary>
    ///     判断是否空
    /// </summary>
    return this.length <= 0;
}

String.prototype.len = function () {
    /// <summary>
    ///     计算string的字节数
    /// </summary>
    return this.replace(/[^\x00-\xff]/g, "xx").length;
}

//判断邮箱格式是否正确  
String.prototype.isEmail = function () {
    return (new RegExp(/^\w+((-\w+)|(\.\w+))*\-AT-[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(this));
}

//判断输入的身份证号是否正确
String.prototype.isIdNo = function () {
    if (isNaN(this)) { return false; }

    var len = this.length, re;
    if (len == 15) {
        re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/);
    } else if (len == 18) {
        re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\d)$/);
    }
    else {
        return false;
    }

    var a = this.match(re);
    if (a != null) {
        if (len == 15) {
            var D = new Date("19" + a[3] + "/" + a[4] + "/" + a[5]);
            var B = D.getYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
        }
        else {
            var D = new Date(a[3] + "/" + a[4] + "/" + a[5]);
            var B = D.getFullYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
        }
        if (!B) { alert("输入的身份证号 " + a[0] + " 里出生日期不对！"); return false; }
    }
    return true;
}

//截断字符串，使用方式: 'Hellooooooo'.cutScope(9);超出9个字符(包括中文)就省略
String.prototype.cutScope = function (max) {
    if (this.length > max)
    { return this.substr(0, max); }
    else
    { return this; }
}

//判断是否是url
String.prototype.isUrl = function () {
    var strRegex = "^((https|http|ftp|rtsp|mms)?://)"
        + "{1}(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@
        + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
        + "|" // 允许IP和DOMAIN（域名）
        + "([0-9a-z_!~*'()-]+\.)*" // 域名- www.
        + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名
        + "[a-z]{2,6})" // first level domain- .com or .museum
        + "(:[0-9]{1,4})?" // 端口- :80
        + "((/?)|" // a slash isn't required if there is no file name
        + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
    var re = new RegExp(strRegex);
    return re.test(this);
}

//显示数字，无小数位，使用逗号分割，如：12300显示为12,300
String.prototype.separateByComma = function () {
    var b = this;
    var len = b.length;
    if (len <= 3) { return b; }
    var r = len % 3;
    return r > 0 ? b.slice(0, r) + "," + b.slice(r, len).match(/\d{3}/g).join(",") : b.slice(r, len).match(/\d{3}/g).join(",");
}
//显示数字,有小数位，使用逗号分割，如：12300显示为12,300 参数1为要格式化的数字，参数2为保留几位小数位
String.prototype.separateByComma2 = function (n) {
    n = n > 0 && n <= 20 ? n : 2;
    s = parseFloat((this + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
    var l = s.split(".")[0].split("").reverse();
    var r = s.split(".")[1];
    var t = "";
    for (i = 0; i < l.length; i++) {
        t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
    }
    return t.split("").reverse().join("") + "." + r;
}

/*去掉文字内容的HTML标签*/
String.prototype.removeHtml = function () {
    if (this == null) return "";
    var str = this;
    //去掉JS
    str = str.replace(/<script.*?>(.|\n)*<\/script>/gi);
    str = str.replace(/<.+?>/g, "");
    //去掉多余空白符号
    str = str.replace(/\s+/g, " ");
    //将换行符更换成回车换行标记
    str = str.replace(/<br>/gi, "\n\r");
    str = str.replace(/<\/br>/gi, "\n\r");
    str = str.replace(/<p>/gi, "\n\r");
    str = str.replace(/<\/p>/gi, "\n\r");
    str = str.replace(/<p( )*\/>/gi, "\n\r");
    str = str.replace(/<br( )*\/>/gi, "\n\r");
    //去掉所有HTML标记
    str = str.replace(/<[^>]+?>/g, " ");
    //去掉空行
    str = str.replace(/\n[\s| ]*\r/gi, "\n\r");
    //将多个连续空格替换成一个空格
    str = str.replace(/( ){2,}/g, " ");
    //去掉首尾空格
    str = str.replace(/(^\s*)|(\s*$)/g, "");
    //去掉行首空格
    str = str.replace(/\n\r( )*/g, "\n\r");
    //去掉行尾空格
    str = str.replace(/( )*\n\r/g, "\n\r");
    return str;
};